package com;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import io.swagger.annotations.ApiOperation;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class tasdasd {

    // 支付宝网关：沙箱环境（真实环境的话改外：https://openapi.alipay.com/gateway.do）
    private static final String URL = "https://openapi.alipaydev.com/gateway.do";
    // APPID（请自行填写， 真实环境请做对应修改）
    private static final String APP_ID = "2021000118693539";
    // 应用私钥（请自行填写，真实环境请做对应修改）
    private static final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCP5ebIt6j5fkyl+J4sr3nrEjidz1DZaUEZ3ZFX8dvM/xXD29asvm/Lye4ZCZeuUte/HFikSGBeMLXwekVXzVcKeUmreJA1ffU2+Nol6LyEbJSoxZ2V0RoW5jTKL47TnH9f1tMd+FBAXPjavVA6i60SaHwWVEiTqpQT/NQR+1wNIHJlHcfhkhWStYUuCSBdha5AaIKshSSCbwUjsnlGqZMgtx/0VZFG5i6JQLQg8If/fXdRnMledi1YPb4BIpoFxlJBVGB9OI+pY9LwaXGvG35FHGlBa7PSAH6HzMMG163fBG/SQ/HQa1idYAFczviqtmDk2DAvHm/vkCuMOc6YYbTNAgMBAAECggEAT2VKMPUlBoPpRqomnAE2A73J/0ikE1E/2kWmLCAGZtVX7PkfObDZflesR6RDTKvo3kmFnx9QDKhTupu8JvXlUKIbQ8EB0IHvCqXDt6byXR6L55Mhn8f/3SKnfec/pwJyRS8CzUZ1AQ5LQGW0HplypPWBgdB/OvqjOWQGnuVRpOunzsjokMuU3CLA2cN28EnC8HdEC46mD+AOceB5Dy3bVChy2flVGgfgFI9gKQ8E2bJ4ixVlRxziFzP7z5o9WFBbdiKryxWd7TrRyu3yHTLAEnNESIihJ3lESUuQT+BCDrZo83pIcSYLeBG5dcHcqOT2/A8s72ndsArYgXyCx+awAQKBgQD2J16R9BvxCNIPFwtOuG94vcXWIuwfPjEc5TdGve6q6ETB84OJubAgV/0MNIu0ICj80qXmG5Pzr7Ns6Xi29bRGP6qnJOIrrW+nkIZ+1+tyswi1/89UV5450Dx7AP2k7XJx2mD9xQ7OjyKyFm1+h7yq9J1Ry7qVwpaWLAqvkHMhEQKBgQCVp21C4qvLc0sD7xae/BOUs5xpdYdzmzF1e8UzjlhIt0mJ7742iWGYL5GrJg5qAy+mAreQmID4fyb0Lp2Pyb5PeNc/I1CpvbRKJPxXzWlhn05xIkF1VtrvhEBa29KojfEzlyCaegqNd5rACw7iC9sgorTqLcNc7/kR4+L4fImX/QKBgG63ypvPtvm1w/YxoU5pOwqOIXxt/NMYwDKFD1XsfHu3OaYbiCDcutHxgcqJoAUHKih9/0z/Po8vin8EjN2OquWmIXZI6spGmqA+K6DGMgtHaDWkuHbxaiQK/Ak/qtcSTpXTaij7B+v1CeAcomd1WnFy0YhHIMfX3K/zY2MQkBhhAoGAOiBouNTv4zrpxSJlR+Ui+dv1gaSrssRxpm8nfknokn3Wa2QaHnan2IOJcc97Q8PvCuz9PYVuepZwm+FLQ6EZcSa/e/+MghFz6GSLOtNG/irxPdIPWLMeoG9ey6ucG+nd6Krnd/i1BpORIMxqwHPYbcmkuVyDCCMRO9imvNsqWkUCgYEA1V2m8GekNNvPkp0qAQQVNZjk+nBHGrtrO9FxzvlVzdT4j3e8ISiJH3ZcEdr0aYx75jf7nVxlUsTwHukTqmYOUtBv37rN1HZHhPAdtWCu4e5A0w8jfudgVM81Un/NGjUCo/2LvlGR6PP5S541+wwgz3pWFh3mpyoUq98nOstCYDU=";
    // 数据返回的格式（只支持json格式）
    private static final String FORMAT = "json";
    // 验签编码（根据需要修改）
    private static final String CHARSET = "UTF-8";
    // 支付宝公钥（请自行填写， 真实环境请做对应修改）
    private static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoHH6l/n41zpiPcGdbc9rADVfZEBRLRHWKZJz9QL3kYzNlbbX/GCehHjHZ1AwOhGaS+GZhOE50LpLlHQC6qTpa/yBE/0LDRKLXdz0zPiHrluWveqVqcxM4AIembAFBeWDiFp2MFnUXPs1dCbWoEZh0owubMqqG6T2oIdm8r/siwKcQEeU6mO25a791woaZlnkl8OhQNKg+e/RDHU2AO7fo1WMk3V7uaK0ovlG9zN6GzvULrM/v0i48NtQZMvL0Desx19veAMVW7Lbnbup36UA0W8rP73CbMBV1kWx95TQ3lwT5Xiahy9CnshZgq6QQjVtLZRtZsoplD0T5+UjaKrksQIDAQAB";
    // 验签加密方法（根据需要修改）
    private static final String SIGN_TYPE = "RSA2";

    @ApiOperation(value = "支付宝支付 沙箱环境")
    @PostMapping("sandboxPay")
    @Test
    public String sandboxPay() throws AlipayApiException {
        AlipayClient alipayClient = new
                DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest();
// 设置支付宝异步通知回调地址（注意：这个网址必须是可以通过外网访问的网址）
        alipayRequest.setNotifyUrl("");
        alipayRequest.setBizContent("{" +
                "\"out_trade_no\" : \"123456\"," +        // 商户订单号
                "\"total_amount\" : \"88.88\"," +            // 商品价格
                "\"subject\" : \"测试\"," +                // 商品标题
                "\"store_id\" : \"公司名\"," +                // 组织或公司名
                "\"timeout_express\" : \"30m\"}");        // 订单有效时间
        AlipayTradePrecreateResponse response = alipayClient.execute(alipayRequest);
// 返回支付宝支付网址，用于生成二维码
        String qr = response.getQrCode();
        System.out.println(qr);
        return qr;

    }
}
